package cn.jg.framework.common.exception;

import cn.jg.framework.common.constant.BizCodeConstant;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

/**
 * @author fanfever
 * @date 2016/7/9
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@JsonIgnoreProperties({"cause", "stackTrace", "localizedMessage", "suppressed", "i18nMessage"})
public class BadRequestException extends BaseException {

	@JsonProperty("message")
	private List<String> messageList;

	private Map<String, Object> data;

	private transient I18nMessage i18nMessage;

	private boolean defaultLocale = false;

	public BadRequestException(String bizCode, String messageCode, String... params) {
		super(bizCode);
		i18nMessage = new I18nMessage(messageCode, params);
		this.messageList = Lists.newArrayListWithCapacity(1);
		this.messageList.add(messageCode);
	}

	public BadRequestException(Boolean b,String message,Object... args) {
		super(BizCodeConstant.UNKNOWN_ERROR);
		this.type = ExceptionType.ERROR;
		this.messageList = Lists.newArrayList();
		this.messageList.add(I18NUtil.getMessage(message, args));
	}


	public BadRequestException(String bizCode, String messageCode, boolean defaultLocale) {
		super(bizCode);
		i18nMessage = new I18nMessage(messageCode, "");
		this.messageList = Lists.newArrayListWithCapacity(1);
		this.messageList.add(messageCode);
		this.defaultLocale = defaultLocale;
	}

	public BadRequestException(Map<String, Object> data, String bizCode, String messageCode, String... params) {
		super(bizCode);
		i18nMessage = new I18nMessage(messageCode, params);
		this.messageList = Lists.newArrayListWithCapacity(1);
		this.messageList.add(messageCode);
		this.setData(data);
	}

	public BadRequestException(String bizCode, List<String> messageList) {
		super(bizCode);
		this.messageList = messageList;
	}

	public BadRequestException(String message) {
		super(BizCodeConstant.UNKNOWN_ERROR);
		this.messageList = Lists.newArrayListWithCapacity(1);
		this.messageList.add(message);
	}

	public BadRequestException(String message, Throwable cause) {
		super(BizCodeConstant.UNKNOWN_ERROR, cause);
		this.messageList = Lists.newArrayListWithCapacity(1);
		this.messageList.add(message);
	}
}
